package com.week.tiny_video.service.impl;

import com.week.tiny_video.dao.VideoDao;
import com.week.tiny_video.pojo.Category;
import com.week.tiny_video.pojo.User;
import com.week.tiny_video.pojo.Video;
import com.week.tiny_video.service.CategoryService;
import com.week.tiny_video.service.UserService;
import com.week.tiny_video.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class VideoServiceImpl implements VideoService {

    @Autowired
    private VideoDao videoDao;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private UserService userService;
    @Override
    public void add(Video video) {
        videoDao.save(video);
    }

    @Override
    public void update(Video video) {
        videoDao.save(video);
    }

    @Override
    public List<Video> findListByCategory(int cid) {
        Category category = categoryService.get(cid);
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        return videoDao.findAllByCategory(category);
    }

    @Override
    public List<Video> findListByUser(int uid) {
        User user = userService.getById(uid);
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        return videoDao.findAllByUser(user);
    }


    @Override
    public Video get(int id) {
        return videoDao.findById(id).orElse(null);
    }

    @Override
    public void delete(int id) {
        videoDao.deleteById(id);
    }

    @Override
    public List<Video> listByStatus(int status) {
        return videoDao.findAllByStatus(status);
    }

    @Override
    public List<Video> listByUserAndStatus(int uid, int status) {
        User user = userService.getById(uid);
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        return videoDao.findAllByUserAndStatus(user,status);
    }
}
